package com.clickpaas.governmentcloud.service.policymatchextservice.impl;

import com.bizcloud.ipaas.tb4093760b8224c4fbcff37d3fa266753.d20210127152100.model.*;
import com.clickpaas.governmentcloud.common.ConstantExt;
import com.clickpaas.governmentcloud.common.ConstantSuggest;
import com.clickpaas.governmentcloud.common.EnumCompanyType;
import com.clickpaas.governmentcloud.common.ServerResponse;
import com.clickpaas.governmentcloud.entity.policyEntity.PolicyEntity;
import com.clickpaas.governmentcloud.service.policymatchextservice.MatchExtService;
import com.clickpaas.governmentcloud.utils.policycondcalutils.PolicyCondCal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @company <上海爱湃斯科技有限公司>
 * @email <zhihui.peng@clickpaas.com>
 * @author huiye
 * @decription: 国家科技型中小企业评价
 * @date 2020/12/16 11:12 上午
 */
@Slf4j
@Service
public class NatTechSMEEvaServiceImpl implements MatchExtService {

    @Resource
    private PolicyCondCal policyCondCal;
    /**
     * 跟政策是否匹配
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse<?> isMatchSuccess(PolicyEntity policy) {
        /**
         * 建议
         */
        StringBuffer suggest = new StringBuffer();

        /**
         * 返回信息
         */
        ServerResponse<?> serverResponse = ServerResponse.createError(ConstantExt._ERROR_MESSAGE);

        /**
         * 评分
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 是否快速匹配：Y->true,N->false
         */
        boolean isQuickMatch = ConstantExt.QUICK_MATCH.equals(policy.getType());
        /**
         *  必要条件判断
         */
        HashMap hashMap = necessCondi(policy);
        /**
         * 必要条件对快速匹配数据做处理（精确匹配、快速匹配）
         */
        boolean ifNecess=Boolean.parseBoolean(hashMap.get(ConstantExt._NECESS).toString());
        if(!ifNecess && isQuickMatch){
            return serverResponse;
        }
        /**
         * 必要条件建议追加
         */
        if (!ifNecess){
            suggest.append(ConstantSuggest._SMES_NECESSARY);
        }
        /**
         * 累加必要条件评分
         */
        score = score + Integer.parseInt(hashMap.get(ConstantExt._SCORE).toString());
        suggest.append(hashMap.get(ConstantExt._SUGGEST).toString());


        HashMap suggAndScore = getScore(policy);
        /**
         * 累加评分
         */
        score = score + Integer.parseInt(suggAndScore.get(ConstantExt._SCORE).toString());
        /**
         * 评分为0，快速匹配不展示数据
         */
        if(score <= 0 && isQuickMatch){
            return serverResponse;
        }
        /**
         * 添加建议
         */
        suggest.append(suggAndScore.get(ConstantExt._SUGGEST).toString());
        /*if (suggest.length() == 0 && score >= 90) {
            suggest.append("建议申报");
        }*/

        /**
         * 快速匹配不添加建议
         */
        if (isQuickMatch){
            suggest = new StringBuffer();
        }
        /**
         * 必要条件不满足得0分
         */
        if(!ifNecess){
            score = 0;
        }
        return insertPolicyData(policy,score,suggest,serverResponse);
    }

    /**
     * 计算企业评分
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap getScore(PolicyEntity policy) {
        HashMap<String, Object> returnValue = new HashMap<>();
        StringBuffer suggest = new StringBuffer();
        /**
         * 分值
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 研发人员占比
         * ≧30%	20分
         * ≧25%	16分
         * ≧20%	12分
         * ≧15%	8分
         * ≧10%	4分
         * <10%	0分
         */
        if (policyCondCal.RDPersonRate(policy)>=30){
            score +=20;
        }else if(policyCondCal.RDPersonRate(policy)>=25){
            score +=16;
        }else if(policyCondCal.RDPersonRate(policy)>=20){
            score +=12;
        }else if(policyCondCal.RDPersonRate(policy)>=15){
            score +=8;
        }else if(policyCondCal.RDPersonRate(policy)>=10){
            score +=4;
        }
        if (policyCondCal.RDPersonRate(policy)<20){
            suggest.append(ConstantSuggest._SMES_RD_PERSONNEL);
        }
        /**
         * 去年研发费用占比
         * ≧6%	50分
         * ≧5%	40分
         * ≧4%	30分
         * ≧3%	20分
         * ≧2%	10分
         * <2%	0分
         */
        if (policyCondCal.RDPerCostRateLastYear(policy)>=6){
            score +=49;
        }else if(policyCondCal.RDPerCostRateLastYear(policy)>=5){
            score +=39;
        }else if(policyCondCal.RDPerCostRateLastYear(policy)>=4){
            score +=29;
        }else if(policyCondCal.RDPerCostRateLastYear(policy)>=3){
            score +=19;
        }else if(policyCondCal.RDPerCostRateLastYear(policy)>=2){
            score +=10;
        }
        if (policyCondCal.RDPerCostRateLastYear(policy)<4){
            suggest.append(ConstantSuggest._SMES_RD_EXPENSES);
        }
        /**
         * 知识产权得分
         * I类≧1  	30分
         * Ⅱ类≧4	24分
         * Ⅱ类≧3	18分
         * Ⅱ类≧2	12分
         * Ⅱ类≧1	6分
         * Ⅱ类<1	0分
         */
        if(policyCondCal.IntellPropertyFirst(policy)>0){
            score+=30;
        }else if(policyCondCal.IntellPropertySecond(policy)>=4){
            score+=24;
        }else if(policyCondCal.IntellPropertySecond(policy)>=3){
            score+=18;
        }else if(policyCondCal.IntellPropertySecond(policy)>=2){
            score+=12;
        }else if(policyCondCal.IntellPropertySecond(policy)>=1){
            score+=6;
        }
        if (policyCondCal.IntellPropertyFirst(policy)<3){
            suggest.append(ConstantSuggest._SMES_INTELLECTUAL);
        }

        returnValue.put(ConstantExt._SCORE,score);
        returnValue.put(ConstantExt._SUGGEST,suggest);
        return returnValue;
    }

    /**
     * 写入匹配结果
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse insertPolicyData(PolicyEntity policy,Integer score,StringBuffer suggest,ServerResponse serverResponse) {
        /**
         * 查询当前政策信息
         */
        TPoliciesDTO policyInfo = policyCondCal.getPolicyInfo(ConstantExt._NATIONAL_TECH_SME_EVALUTION);

        if (null == policyInfo) {
            log.info("政策：【{}】查询不到",ConstantExt._NATIONAL_TECH_SME_EVALUTION);
            return serverResponse;
        }
        policyCondCal.insertCommonPolicyData(policy, policyInfo, score, suggest);
        return ServerResponse.createSuccess(ConstantExt._SUCCESS_MESSAGE);
    }

    /**
     * 必要条件判断
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap necessCondi(PolicyEntity policy) {
        HashMap<String, Object> map = new HashMap<>();
        Integer score = ConstantExt._ZERO;
        map.put(ConstantExt._NECESS,true);
        StringBuffer suggStr = new StringBuffer();
        /**
         * 是否快速匹配：Y->true,N->false
         */
        boolean isQuickMatch = ConstantExt.QUICK_MATCH.equals(policy.getType());
        if(isQuickMatch && null == policy.getDishonesty()){
            //TODO
        }else{
            /**
             * 是否违法
             */
            if(!policyCondCal.isIllegal(policy)){
                map.put(ConstantExt._NECESS,false);
            }
        }


        if(isQuickMatch && null == policy.getEnterpriseScale()){
            //TODO
        }else {
            /**
             * 是否中小企业：
             */
            if(!policyCondCal.ifMediumOrMiniComp(policy)){
                map.put(ConstantExt._NECESS,false);
            }
        }

        if (isQuickMatch && (null == policy.getRdInstitution()
                || null == policy.getEnterpriseQualification()
                || null == policy.getParticipateDevelopment())){
            //TODO
        }else {
            /**
             * 拥有有效期内高新技术企业资格证书；(资质)
             * 企业近5年内获得过国家级科技奖励，并在获奖单位中排在前3名；
             * 企业拥有经认定的省部级以上研发机构；字段
             * 企业近5年内主导制定过国际标准、国家标准或行业标准。>0
             */
            if(null != policy.getRdInstitution()
                    && null != policy.getEnterpriseQualification()
                    && null != policy.getParticipateDevelopment()
                    && ConstantExt._YES.equals(policy.getRdInstitution())
                    && policy.getEnterpriseQualification().contains(EnumCompanyType._HIGHTECHENTER.getCompanyTypeChina())
                    && policy.getParticipateDevelopment()>0){

            }else {
                map.put(ConstantExt._NECESS,false);
            }
        }

        map.put(ConstantExt._SCORE,score);
        map.put(ConstantExt._SUGGEST,suggStr);
        return map;
    }
}
